package com.ynet.hmds28.user.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.boot.hsf.annotation.HSFProvider;
import com.ynet.hm.user.domain.UserBean;
import com.ynet.hm.user.service.OpenPlatformService;
import com.ynet.hm.utils.UuidUtil;
import com.ynet.hm.utils.config.Result;
import com.ynet.hm.utils.config.ResultUtil;
import com.ynet.hm.utils.enums.ExceptionEnum;
import com.ynet.hm.utils.exception.MyException;
import com.ynet.hm.utils.rsa.RSAHelper;
import com.ynet.hmds28.user.dao.PlatflowDao;
import com.ynet.hmds28.user.dao.ServiceconsumeDao;
import com.ynet.hmds28.user.dao.ServiceprovideDao;
import com.ynet.hmds28.user.dao.UserDao;
import com.ynet.hmds28.user.model.User;
import com.ynet.hmds28.user.model.UserQuery;
import com.ynet.hmds28.user.redis.RedisService;
@Transactional
@HSFProvider(serviceInterface = OpenPlatformService.class, serviceVersion = "1.0.0",serviceGroup = "hmds28")
public class OpenPlatformServiceImpl implements OpenPlatformService {
	@Autowired
	private UserDao userDao;
	@Autowired	
	private ServiceconsumeDao serviceconsumeDao;
	@Autowired
	private PlatflowDao platflowDao;
	@Autowired
	private ServiceprovideDao serviceprovideDao;
	@Autowired
	private RedisService redisService;
//	@Value("${userId}")
//	public String userId;
	
	@Override
	public UserBean openPlatformLogin(String userName, String password) {
		UserQuery example=new UserQuery();
		example.createCriteria().andUsernameEqualTo(userName).andPasswordEqualTo(password);
		List<User> users=userDao.selectByExample(example);
		if(users == null || users.size() == 0){
			throw new MyException(ExceptionEnum.USER_NOT_FIND);
		}
		User user=users.get(0);
		if(!"0".equals(user.getStatus())){
			throw new MyException(ExceptionEnum.USER_NOT_NOMAL);
		}
		UserBean userBean = new UserBean();
		
		userBean.setCreatetime(user.getCreatetime());
		userBean.setPassword(user.getPassword());
		userBean.setRemark(user.getRemark());
		userBean.setUserid(user.getUserid());
		userBean.setUsername(user.getUsername());
		userBean.setUsertype(user.getUsertype());
		userBean.setStatus(user.getStatus());
		return userBean;
	}
	@Override
	public Result<?> openPlatformRegister(String userName, String password,String platformPublic) {
		//开启GTS事务
		userDao.enableTXC();
		UserQuery example=new UserQuery();
		example.createCriteria().andUsernameEqualTo(userName);
		List<User> users=userDao.selectByExample(example);
		if(!CollectionUtils.isEmpty(users)){
			throw new MyException(ExceptionEnum.USER_IS_EXIST);
		}
		//为商户生产公钥私钥
		Map keyMap = RSAHelper.generateKeyPair();
		String userPrivate=(String) keyMap.get(RSAHelper.PRIVATE_KEY);
		String userPublic=(String) keyMap.get(RSAHelper.PUBLIC_KEY);
		System.out.println("======================="+userPrivate+"=====================");
		User user=new User();
		
		String userId = UuidUtil.getUUid();
		
		if(userDao.selectByPrimaryKey("fx001")!=null){
			if(userDao.selectByPrimaryKey("fx002")==null){
				userId = "fx002";
			}
		}else{
			userId = "fx001";
		}
		user.setUserid(userId);
		user.setCreatetime(new Date());
		user.setPassword(password);
		user.setStatus("0");
		user.setUsername(userName);
		user.setUsertype("0");
		user.setUserprivate(userPrivate);
		user.setUserpublic(userPublic);
		user.setPlatepublic(platformPublic);
		int i = userDao.insert(user);
		if(1==i){
			redisService.set(userId+"privateKey", userPrivate);
			redisService.set(userId+"publicKey", userPublic);
		}
		return ResultUtil.success();
	}

}
